package in.abip.chisame;

import in.abip.chisame.model.Room;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Room activity.
 * 
 * @author Julot.
 * @since Thursday, January 26th, 2011.
 */
public class RoomListActivity extends ChisameActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);
        populate();
        bind();
    }
    
    /** Populate room list by item from database. */
    protected void populate() {
        ListView list = (ListView) findViewById(R.id.list);
        ListAdapter adapter = new ListAdapter(
    		this,
    		R.layout.list_item,
    		Room.model().findAll()
		);
        list.setAdapter(adapter);
    }
    
    /** Bind room list view to listener. */
	private void bind() {
        OnItemClickListener listener = new OnItemClickListener() {
			public void onItemClick(
				AdapterView<?> parent,
				View itemClicked,
				int position,
				long id
			) {
				ListAdapter adapter;
				adapter = (ListAdapter) parent.getAdapter();
				Room room = (Room) adapter.getItem(position);
				Bundle bundle = new Bundle();
				bundle.putInt("roomId", room._id);
				Intent intent = new Intent(
					RoomListActivity.this,
					RoomTabActivity.class
				);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		};
        ListView list = (ListView) findViewById(R.id.list);
		list.setOnItemClickListener(listener);
	}
}
